import { Injectable } from '@angular/core';

import 'rxjs/add/operator/toPromise';
import {Observable} from "rxjs/Observable";
import {SimplePendingTask} from "../shared/PendingTask";

@Injectable()
export class BackApiService {

  private PENDING_TASKS: SimplePendingTask[]= [
  {
    id: 1,
    no: '10000',
    name: '习近平',
    phone: '8888888',
    address: '天安门广场'
  },
  {
    id: 2,
    no: '10001',
    name: '王岐山',
    phone: '8888888',
    address: '天安门广场'
  },
  {
    id: 3,
    no: '10002',
    name: '张高丽',
    phone: '8888888',
    address: '天安门广场'
  },
  {
    id: 4,
    no: '10003',
    name: '刘云山',
    phone: '8888888',
    address: '天安门广场'
  },
  {
    id: 5,
    no: '10004',
    name: '李克强',
    phone: '8888888',
    address: '天安门广场'
  },
  {
    id: 6,
    no: '10005',
    name: '张德江',
    phone: '8888888',
    address: '天安门广场'
  },
  {
    id: 7,
    no: '10006',
    name: '俞正声',
    phone: '8888888',
    address: '天安门广场'
  },
  {
    id: 8,
    no: '10007',
    name: '傅政华',
    phone: '8888888',
    address: '天安门广场'
  },
  {
    id: 9,
    no: '10008',
    name: '姐夫',
    phone: '8888888',
    address: '天安门广场'
  },
];


  constructor() { }

  getFinishs(): Promise<SimplePendingTask[]> {
    return this.getPendings();
  }

  getProgressings(): Promise<SimplePendingTask[]> {
    return this.getPendings();
  }

  getProgressing(id: number): Promise<SimplePendingTask> {
    return this.getPending(id)
  }

  // 待办业务列表// 可能需要处理动态加载 或者分页
  getPendings(): Promise<SimplePendingTask[]> {
    return Observable.of(this.PENDING_TASKS)
      .toPromise().catch(this.handleError);
  }

  // 根据id 获取 待办业务细节
  getPending(no: number): Promise<SimplePendingTask> {

    for (let item of this.PENDING_TASKS) {
      console.log("id: "+ item.id);
      console.log("input no: "+ no);

      if (item.id == no) {
        return Observable.of(item).toPromise()
          .catch(this.handleError);
      }
    }

    console.log("can't find pendingTask: "+ no);
  }

  //noinspection JSMethodCanBeStatic
  private handleError(error: any): Promise<any> {
    console.error("An error occurred", error);
    return Promise.reject(error.message || error);
  }

}
